package com.link.base.base.redpacketconfig.service;

import javax.annotation.Resource;

import com.link.base.base.member.util.MpPayConstants;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.RedisUtil;
import com.link.core.util.UserUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;
import com.link.base.base.redpacketconfig.dao.mybatis.mapper.RedPktDrawCfgMapper;
import com.link.base.base.redpacketconfig.model.RedPktDrawCfg;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;


/**
 * @author: 闫有霞
 * @date: 2019-01-23
 * @comments: 红包领取设置
 */
@Service
public class RedPktDrawCfgServiceImpl extends BasicServiceImpl<RedPktDrawCfg> implements RedPktDrawCfgService {

    @Resource
    private RedPktDrawCfgMapper redPktDrawCfgMapper;

    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicMapper<RedPktDrawCfg> getBasicMapper() {
        return redPktDrawCfgMapper;
    }


    /**
     *@createdBy 闫有霞
     *@date 2019/1/23
     *@methods: loadDrawCfgToRedis
     *@param:   RedPktDrawCfg entity
     *@return:  void
     *@description: 加载红包领取设置数据到Redis
     **/

    @Override
    public void loadDrawCfgToRedis(RedPktDrawCfg entity) throws Exception {
        LinkRedisClient jedis = null;
        try {
            // 红包领取设置
            jedis = RedisUtil.getJedis();

            // 单个红包最大限额
            jedis.hset(MpPayConstants.RED_ENVELOPES_CONFIG_KEY, MpPayConstants.RED_ENVELOPES_MAX_PRICE, String.valueOf(entity.getSingleMaxAmount()));
            //红包领取伪造请求数量
            jedis.hset(MpPayConstants.RED_ENVELOPES_CONFIG_KEY, MpPayConstants.RED_ENVELOPES_FORGERY_REQUEST_NUM,
                    String.valueOf(entity.getUnusualReqNum()));
            //红包领取伪造请求时间间隔
            jedis.hset(MpPayConstants.RED_ENVELOPES_CONFIG_KEY, MpPayConstants.RED_ENVELOPES_FORGERY_REQUEST_TIME_INTERVAL,
                    String.valueOf(entity.getUnusualReqLockTime()));
            //红包领取异常通知信息
            jedis.hset(MpPayConstants.RED_ENVELOPES_CONFIG_KEY, MpPayConstants.RED_ENVELOPES_FORGERY_REQUEST_ALERT_MSG,
                    entity.getUnusualReqHint());
        } catch (Exception e) {
            LCLogger.withException(e);
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    @Override
    public void insertDrawCfg(RedPktDrawCfg entity) throws Exception {
        if (entity != null) {
            Long id = keyGenerateService.keyGenerate();
            Long corpid = UserUtil.getUser().getLoginCorpId();
            entity.setId(id);
            entity.setCorpid(corpid);
            redPktDrawCfgMapper.insert(entity);
            loadDrawCfgToRedis(entity);
        }
    }

    @Override
    public void updateDrawCfg(RedPktDrawCfg entity) throws Exception {
        if (entity != null) {
            redPktDrawCfgMapper.update(entity);
            loadDrawCfgToRedis(entity);
        }
    }
}

